<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="110px" :rules="dynamicFormRules">

                <el-form-item label="应用编号" v-show="isEditShow" prop="id">
                    <el-input v-model="formData.id" disabled placeholder="请输入主键" />
                </el-form-item>
                <el-form-item label="应用名称" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入应用名称" />
                </el-form-item>
                <el-form-item label="应用标识" prop="package_name">
                    <el-input
                        v-model="formData.package_name"
                        placeholder="请输入应用标识"
                    />
                </el-form-item>
                <el-form-item label="小程序AppId" prop="app_id">
                    <el-input v-model="formData.app_id" placeholder="请输入唯一标识" />
                </el-form-item>
                <el-form-item label="小程序Secret" prop="app_secret">
                    <el-input v-model="formData.app_secret" placeholder="请输入小程序密钥" />
                </el-form-item>
                <el-form-item label="版本号" prop="version">
                    <el-input v-model="formData.version" placeholder="请输入版本号" />
                </el-form-item>
                <el-form-item label="支付渠道标识" prop="pay_flags">
                    <el-select class="w-[280px]" multiple v-model="formData.pay_flags">
                        <el-option
                            v-for="item in optionsData.articleCate"
                            :key="item.flag"
                            :label="item.title"
                            :value="item.flag"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="formData.status" placeholder="请选择状态">
                        <el-radio :label="0">禁用</el-radio>
                        <el-radio :label="1">启用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  configsEdit, configsAdd, configsDetail } from '@/api/client/app_configs'
import Popup from '@/components/popup/index.vue'
import { configsAll } from '@/api/client/pay_configs'
import { useDictOptions } from '@/hooks/useDictOptions'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑应用配置' : '新增应用配置'
})

const formData = reactive({
    id: '',
    title: '',
    package_name: '',
    app_id: '',
    app_secret: '',
    version: '',
    pay_flag: '',
    status: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入主键',
            trigger: ['blur']
        }
    ],
    title: [
        {
            required: true,
            message: '请输入应用名称',
            trigger: ['blur']
        }
    ],
    package_name: [
        {
            required: true,
            message: '请输入应用标识',
            trigger: ['blur']
        }
    ],
    app_id: [
        {
            required: true,
            message: '请输入唯一标识',
            trigger: ['blur']
        }
    ],
    app_secret: [
        {
            required: true,
            message: '请输入小程序密钥',
            trigger: ['blur']
        }
    ],
    version: [
        {
            required: true,
            message: '请输入版本号',
            trigger: ['blur']
        }
    ],
    pay_flags: [
        {
            required: false,
            message: '请输入支付渠道',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择',
            trigger: ['blur']
        }
    ],
}

// 修改为动态规则
const dynamicFormRules = computed(() => {
    const rules = { ...formRules }

    // 在添加模式下，account_id 不需要必填
    if (mode.value === 'add') {
        rules.id = [
            {
                required: false,
                message: '请输入主键',
                trigger: ['blur']
            }
        ]
    }

    return rules
})

const isEditShow = computed(() => mode.value === 'edit')

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await configsEdit(data) : await configsAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

// 存储支付配置数据
const { optionsData } = useDictOptions<{
    articleCate: any[]
}>({
    articleCate: {
        api: configsAll
    }
})

const getDetail = async (row: Record<string, any>) => {
    const data = await configsDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
